package com.basic.library.utils

import android.graphics.Color
import android.graphics.Typeface
import android.text.Spannable
import android.text.SpannableString
import android.text.SpannableStringBuilder
import android.text.style.*

/**
 * @author yj
 * @description
 */
object SpannedUtil {
    /**
     * 超链接
     */
    private fun addUrlSpan() {
        val spanString = SpannableString("超链接")
        val span = URLSpan("tel:0123456789")
        spanString.setSpan(span, 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
    }

    /**
     * 文字背景颜色
     */
    private fun addBackColorSpan() {
        val spanString = SpannableString("背景色")
        val span = BackgroundColorSpan(Color.YELLOW)
        spanString.setSpan(span, 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
    }

    /**
     * 文字颜色
     */
    private fun addForeColorSpan() {
        val spanString = SpannableString("字体色")
        val span = ForegroundColorSpan(Color.BLUE)
        spanString.setSpan(span, 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
    }

    /**
     * 字体大小
     */
    private fun addFontSpan() {
        val spanString = SpannableString("36号字体")
        val span = AbsoluteSizeSpan(36)
        spanString.setSpan(span, 0, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
    }

    /**
     * 粗体，斜体
     */
    private fun addStyleSpan() {
        val spanString = SpannableString("BIBI")
        val span = StyleSpan(Typeface.BOLD_ITALIC)
        spanString.setSpan(span, 0, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
    }

    /**
     * 删除线
     */
    private fun addStrikeSpan() {
        val spanString = SpannableString("删除线")
        val span = StrikethroughSpan()
        spanString.setSpan(span, 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
    }

    /**
     * 下划线
     */
    private fun addUnderLineSpan() {
        val spanString = SpannableString("下划线")
        val span = UnderlineSpan()
        spanString.setSpan(span, 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
    }

    /**
     * 图片
     */
    private fun addImageSpan() {
        val spanString = SpannableString(" ")
        //        Drawable d = getResources().getDrawable(R.drawable.ic_launcher);
//        d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
//        ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
//        spanString.setSpan(span, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    /**
     * 混合
     */
    private fun addConbine() {
        val spannable = SpannableStringBuilder("组合运用啊")
        val span1: CharacterStyle = BackgroundColorSpan(Color.BLUE)
        val span2: CharacterStyle = ForegroundColorSpan(Color.RED)
        spannable.setSpan(span1, 0, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
        spannable.setSpan(span2, 2, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
    }

    fun getSpanned(spannedBeans: ArrayList<SpannedBean>): CharSequence {
        val spannable = SpannableStringBuilder("")
        var start = 0
        spannedBeans.forEach {
            it.text?.run {
                val length = length + start
                spannable.append(it.text)
                it.textSize?.run {
                    spannable.setSpan(AbsoluteSizeSpan(this), start, length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
                }
                it.textColor?.run {
                    spannable.setSpan(ForegroundColorSpan(this), start, length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
                }
                it.textStyle?.run {
                    spannable.setSpan(StyleSpan(this), start, length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
                }
                it.background?.run {
                    spannable.setSpan(BackgroundColorSpan(this), start, length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
                }
                it.href?.run {
                    spannable.setSpan(URLSpan(it.text), start, length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
                }
                start = length
            }
        }
        return spannable
    }
}

class SpannedBean(
        val text: String? = "",
        val textSize: Int? = null,
        val textColor: Int? = null,
        val textStyle: Int? = null,
        val background: Int? = null,
        val href: Boolean? = null
)